package util;
import dao.impl.CaseHistoryDaoImpl;
import entity.CaseHistory;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author:
 * @Date: 2020-05-10
 * @Description: @PACKAGE:util
 * @version: 1.0
 */
public class DataCountUtil {

    public static List doctorData(String id) {
        /**
         * 功能描述:
         *@param: Doctor id
         *@return: List
         *@author:
         *@date: 2020-05-10 19:24
         */

        String listCount = "";
//        int n = 0;
        int hospitalized = 0;//住院

        int number = 0;
        int cure = 0;//治愈
        String scope = "";//范围
        int number2 = 0;
        int total = 0;

        CaseHistoryDaoImpl caseHistoryDao = new CaseHistoryDaoImpl();
        List<String> list = new ArrayList<String>();//不重复疾病表
        List<String> list1 = new ArrayList<String>();//全部疾病
        List<CaseHistory> listDatech = caseHistoryDao.getByDoctorId(id);//数据库病历表


        for (int i = 0; i < listDatech.size(); i++) {//

            list1.add(listDatech.get(i).getCh_clinicaldiagnosis());//

//            if ("1".equals(listDatech.get(i).getCh_hospitalization())) {
//                hospitalized += hospitalized;
//            }
//            if (list.indexOf(listDatech.get(i).getCh_clinicaldiagnosis()) == -1) {
//                list.add(listDatech.get(i).getCh_clinicaldiagnosis());
//            }
        }

        list = list1.stream().distinct().collect(Collectors.toList());


        int a = Collections.frequency(list1, "流行性感冒");
        return null;
    }

    public static int count(String ill, List list) {

        int num = 0;

        for (int j = 0; j < list.size(); j++) {

            if (ill.equals(list.get(j))) {

                num += 1;
            }

        }

        return num;
    }

    public static String bigIllName(String id) {
        int num = 0;
        int num1 = 0;//最高发疾病次数
        String name = "";
        String name1 = "";//最高发疾病
        CaseHistoryDaoImpl caseHistoryDao = new CaseHistoryDaoImpl();
        List<String> list = new ArrayList<String>();//不重复疾病表
        List<String> list1 = new ArrayList<String>();//全部疾病
        List<CaseHistory> listDatech = caseHistoryDao.getByDoctorId(id);//数据库病历表
        for (int i = 0; i < listDatech.size(); i++) {//

            list1.add(listDatech.get(i).getCh_clinicaldiagnosis());

        }
        list = list1.stream().distinct().collect(Collectors.toList());
        for (int i = 0; i < list.size(); i++) {

            name = list.get(i);
            num = DataCountUtil.count(name, list1);

            if (num1 > num) {

            } else {
                num1 = num;
                name1 = name;
            }

        }
        System.out.println(name1 + ":" + num1);

        return name1;
    }

    
    public static int Inpatientpercentage(String id) { //住院/总人数-百分比
        CaseHistoryDaoImpl caseHistoryDao = new CaseHistoryDaoImpl();
        List<String> list1 = new ArrayList<String>();//全部症状显示
        List<CaseHistory> listDatech = caseHistoryDao.getByDoctorId(id);
        for (int i = 0; i < listDatech.size(); i++) {//
            list1.add(listDatech.get(i).getCh_Degree_symptoms());
        }
        int normaled = Collections.frequency(list1, "正常");
        int cured = Collections.frequency(list1, "已治愈");
        int milded = Collections.frequency(list1, "轻度");
        int sum = list1.size();
        float lived = sum - normaled - cured - milded;
        float data = (lived / sum) * 100;
        DecimalFormat decimalFormat = new DecimalFormat("0");
        int data1 = Integer.parseInt(decimalFormat.format(data));
        return data1;
    }

    public static int bigillpercentage(String id) {//近一个月最高发疾病及占比
        int num = 0;
        int num1 = 0;//最高发疾病次数
        String name = "";
        String name1 = "";//最高发疾病
        CaseHistoryDaoImpl caseHistoryDao = new CaseHistoryDaoImpl();
        List<String> list = new ArrayList<String>();//不重复疾病表
        List<String> list1 = new ArrayList<String>();//全部疾病
        List<CaseHistory> listDatech = caseHistoryDao.getByDoctorId(id);//数据库病历表
        for (int i = 0; i < listDatech.size(); i++) {//

            list1.add(listDatech.get(i).getCh_clinicaldiagnosis());

        }
        list = list1.stream().distinct().collect(Collectors.toList());
        for (int i = 0; i < list.size(); i++) {

            name = list.get(i);
            num = DataCountUtil.count(name, list1);

            if (num1 > num) {

            } else {
                num1 = num;
                name1 = name;
            }

        }
        float sum = list1.size();
        float all_ill = num1 / sum;
        float data = all_ill * 100;
        DecimalFormat decimalFormat = new DecimalFormat("0");
        int data1 = Integer.parseInt(decimalFormat.format(data));
        return data1;
    }

    public static int curedpersentage(String id) {   //住院的重症患者治愈率
        CaseHistoryDaoImpl caseHistoryDao = new CaseHistoryDaoImpl();
        List<String> list1 = new ArrayList<String>();//全部症状显示
        List<CaseHistory> listDatech = caseHistoryDao.getByDoctorId(id);
        for (int i = 0; i < listDatech.size(); i++) {//
            list1.add(listDatech.get(i).getCh_Degree_symptoms());
        }
        float cured = Collections.frequency(list1, "已治愈");
        float sum = list1.size();
        float cured_data = cured / sum;
        float data = cured_data * 100;
        DecimalFormat decimalFormat = new DecimalFormat("0");
        int data1 = Integer.parseInt(decimalFormat.format(data));
        return data1;
    }

    public static String ageif(String id) {
        CaseHistoryDaoImpl caseHistoryDao = new CaseHistoryDaoImpl();
        String birthday;
        int num = 0;
        int num1 = 0;//最高发疾病次数
        String name = "";
        String name1 = "";//最高发疾病
        List<String> list1 = new ArrayList<String>();//全部症状显示
        List<String> list = new ArrayList<String>();//不重复疾病表
        List<String> list_age = new ArrayList<String>();
        List<CaseHistory> listDatech = caseHistoryDao.getByDoctorId(id);
        for (int i = 0; i < listDatech.size(); i++) {//
            list1.add(listDatech.get(i).getCh_patientid());
        }
        System.out.println(list1);
        list = list1.stream().distinct().collect(Collectors.toList());
        System.out.println(list);
        for (int i = 0; i < list.size(); i++) {
            birthday = list.get(i).substring(6, 14);
            birthday = birthday.substring(0, 4) + "-" + birthday.substring(4, 6) + "-" + birthday.substring(6, 8);
            long age = DateUtil.getDays(DateUtil.getStringDateShort(), birthday);
            age /= 365;
            String ages = String.valueOf(age);
            list_age.add(ages);
        }
        for (int i = 0; i < list_age.size(); i++) {
            name = list_age.get(i);
            num = DataCountUtil.count(name, list_age);

            if (num1 > num) {

            } else {
                num1 = num;
                name1 = name;
            }
        }
        int age_area = Integer.parseInt(name1);
        if (age_area > 0 && age_area <= 10) {
            return "0岁-10岁";
        }
        if (age_area > 10 && age_area <= 20) {
            return "11岁-20岁";
        }
        if (age_area > 20 && age_area <= 30) {
            return "21岁-30岁";
        }
        if (age_area > 30 && age_area <= 40) {
            return "31岁-40岁";
        }
        if (age_area > 40 && age_area <= 50) {
            return "41岁-50岁";
        }
        if (age_area > 50 && age_area <= 60) {
            return "51岁-60岁";
        }
        if (age_area > 60 && age_area <= 70) {
            return "61岁-70岁";
        }
        if (age_area > 70 && age_area <= 80) {
            return "71岁-80岁";
        }
        if (age_area > 80 && age_area <= 90) {
            return "81岁-90岁";
        } else {
            return "91岁以上";
        }

    }

    public static int maxage(String id) {
        CaseHistoryDaoImpl caseHistoryDao = new CaseHistoryDaoImpl();
        String birthday;
        int num = 0;
        int num1 = 0;//最高发疾病次数
        String name = "";
        String name1 = "";//最高发疾病
        List<String> list1 = new ArrayList<String>();//全部症状显示
        List<String> list = new ArrayList<String>();//不重复疾病表
        List<String> list_age = new ArrayList<String>();
        List<CaseHistory> listDatech = caseHistoryDao.getByDoctorId(id);
        for (int i = 0; i < listDatech.size(); i++) {//
            list1.add(listDatech.get(i).getCh_patientid());
        }
//        System.out.println(list1);
        list = list1.stream().distinct().collect(Collectors.toList());
//        System.out.println(list);
        for (int i = 0; i < list.size(); i++) {
            birthday = list.get(i).substring(6, 14);
            birthday = birthday.substring(0, 4) + "-" + birthday.substring(4, 6) + "-" + birthday.substring(6, 8);
            long age = DateUtil.getDays(DateUtil.getStringDateShort(), birthday);
            age /= 365;
            String ages = String.valueOf(age);
            list_age.add(ages);
        }
        for (int i = 0; i < list_age.size(); i++) {
            name = list_age.get(i);
            num = DataCountUtil.count(name, list_age);

            if (num1 > num) {

            } else {
                num1 = num;
                name1 = name;
            }
        }

        return num1;

    }
    public static List<String> removeDuplicate(List<String> list) {
    	for(int i=0;i<list.size()-1;i++){
			for(int j=0;j<list.size()-1;j++){
				if(list.get(j).equals(list.get(i))){
					list.remove(j);
				}
			}
		}	
    	return list;
    }
}
